package com.xinnet.mms.web;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xinnet.core.utils.page.PageView;
import com.xinnet.mms.dept.service.DeptService;
import com.xinnet.mms.entity.Dept;
import com.xinnet.mms.entity.Emp;
import com.xinnet.mms.entity.Role;
import com.xinnet.mms.login.service.EmpService;
import com.xinnet.mms.role.service.RoleService;
import com.xinnet.mms.utils.AgainPageView;

@Controller
@RequestMapping(value = "/ruleSet/approval/employeeSetApproval")
public class EmployeeSetApprovalController {
	
	@Autowired
	EmpService empService;
	@Autowired
	DeptService deptService;
	@Autowired
	RoleService roleService;
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/employeeSetApprovalPage")
	public String employeeSetApprovalPage(HttpServletRequest request,HttpServletResponse response){
		Emp currentUser = (Emp)request.getSession().getAttribute("currentUser");
		String deptId = currentUser.getId();
		String deptIdStr = deptService.getChildNodeId(deptId);
		List<Emp> empList = empService.getEmpSetApprovalListByDeptIdStr(deptIdStr);
		
		String pageNow = request.getParameter("pageNow");
		PageView pageView = new PageView();
		AgainPageView againPageView = new AgainPageView();
		if(empList!=null){
			pageView.setPageSize(10);
			pageView.setQueryResult(empList.size(), empList);
		}
		if(pageNow==null || "".equals(pageNow)){
			againPageView.againComputePageNowRecords(pageView,1,empList);
		}else{
			againPageView.againComputePageNowRecords(pageView,Integer.parseInt(pageNow),empList);
		}
		List<Dept> deptList = deptService.getDeptListByDeptIdStr(deptIdStr);
		List<Role> roleList = roleService.getRoleList();
		request.setAttribute("deptList", deptList);
		request.setAttribute("roleList", roleList);
		request.setAttribute("pageView", pageView);
		String deptJson  = deptService.getDeptTree();
		request.setAttribute("deptJson", deptJson);
		request.setAttribute("isQuery", false);

		return "/ruleSet/approval/employeeSetApproval";
	}
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/getEmpList")
	public String getEmpList(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
		Emp currentUser = (Emp)request.getSession().getAttribute("currentUser");
		String deptartmentId = currentUser.getId();
		String deptIdStr = deptService.getChildNodeId(deptartmentId);
		
		request.setCharacterEncoding("utf-8");
		String ename = request.getParameter("ename");
		String contact = request.getParameter("contact");
		String mail = request.getParameter("mail");
		String roleId = request.getParameter("roleId");
		String deptId = request.getParameter("deptId");
		Emp emp =  new Emp();
		if(ename!=null && !"".equals(ename)){
			emp.setEname(ename);
		}
		if(roleId!=null && !"".equals(roleId)){
			emp.setRolId(roleId);
		}
		if(deptId!=null && !"".equals(deptId)){
			String tempDeptIdStr = deptService.getChildNodeId(deptId);
			emp.setDeptIdStr(tempDeptIdStr);
			Dept dept = deptService.getDeptByDeptId(deptId);
			request.setAttribute("dept", dept);
		}else{
			emp.setDeptIdStr(deptIdStr);	
		}
		emp.setApprovalStatus(2);
		String pageNow = request.getParameter("pageNow");
		PageView pageView = new PageView();
		AgainPageView againPageView = new AgainPageView();
		List<Dept> deptList = deptService.getDeptListByDeptIdStr(deptIdStr);
		List<Emp> empList = empService.getEmpList(emp);
		if(empList!=null){
			pageView.setPageSize(10);
			pageView.setQueryResult(empList.size(), empList);
		}
		if(pageNow==null || "".equals(pageNow)){
			againPageView.againComputePageNowRecords(pageView,1,empList);
		}else{
			againPageView.againComputePageNowRecords(pageView,Integer.parseInt(pageNow),empList);
		}
		
		List<Role> roleList = roleService.getRoleList();
		request.setAttribute("isQuery", true);
		request.setAttribute("pageView", pageView);
		if(!"".equals(roleId)){
			request.setAttribute("roleId", roleId);
		}
		if(!"".equals(deptId)){
			request.setAttribute("deptId", deptId);
		}
		if(!"".equals(ename)){
			request.setAttribute("ename", ename);
		}
		request.setAttribute("contact", contact);
		request.setAttribute("mail", mail);
		request.setAttribute("deptList", deptList);
		request.setAttribute("roleList", roleList);
		String deptJson  = deptService.getDeptTree();
		request.setAttribute("deptJson", deptJson);
		return "/ruleSet/approval/employeeSetApproval";
	}
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/approval")
	public String approval(HttpServletRequest request,HttpServletResponse response){
		String empno = request.getParameter("empno");
		Emp empParm =  new Emp();
		empParm.setEmpno(empno);
		List<Emp> empList = empService.getEmpList(empParm);
		Emp tempEmp = empList.get(0);
		Integer customerLimit = tempEmp.getCustomerLimitApproval();
		Integer customerLowerLimit = tempEmp.getCustomerLowerLimitApproval();
		Integer isArtificialPush = tempEmp.getIsArtificialPushApproval();
		Integer isSysPush = tempEmp.getIsSysPushApproval();
		Integer repcustomerCount = tempEmp.getRepcustomerCountApproval();
		Integer receiveCustom = tempEmp.getReceiveCustomApproval();
		String approvalStatus = request.getParameter("approvalStatus");
		if(customerLimit == null) {
			customerLimit=tempEmp.getCustomerLimit();
		}
		Integer surplusLimit = customerLimit+(tempEmp.getSurplusLimit()-tempEmp.getCustomerLimit());
		if(approvalStatus==null || "".equals(approvalStatus)){
			return "redirect:/ruleSet/approval/employeeSetApproval/employeeSetApprovalPage";
		}
		Emp emp = new Emp();
		emp.setEmpno(empno);
		if("1".equals(approvalStatus)){
			emp.setRepcustomerCount(repcustomerCount);
			emp.setCustomerLimit(customerLimit);
			emp.setCustomerLowerLimit(customerLowerLimit);
			emp.setIsSysPush(isSysPush);
			emp.setIsArtificialPush(isArtificialPush);
			emp.setReceiveCustom(receiveCustom);
			emp.setSurplusLimit(surplusLimit);
			//emp.setReceiveCustomApproval(receiveCustom);
		}
		emp.setApprovalStatus(Integer.parseInt(approvalStatus));
		empService.updateEmp(emp);	
		return "redirect:/ruleSet/approval/employeeSetApproval/employeeSetApprovalPage";
	}
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/bathApproval")
	public String bathApproval(HttpServletRequest request,HttpServletResponse response){
		String empnoStr = request.getParameter("empnoStr");
		if(empnoStr==null || "".equals(empnoStr)){
			return "redirect:/ruleSet/approval/employeeSetApproval/employeeSetApprovalPage";
		}
		String approvalStatus = request.getParameter("approvalStatus");
		if(approvalStatus==null || "".equals(approvalStatus)){
			return "redirect:/ruleSet/approval/employeeSetApproval/employeeSetApprovalPage";
		}
		String[] empnoStrArray = empnoStr.split(",");
		for (int i = 0; i < empnoStrArray.length; i++) {
			String empno = empnoStrArray[i];
			Emp emp = new Emp();
			emp.setEmpno(empno);
			Emp empParm =  new Emp();
			empParm.setEmpno(empno);
			List<Emp> empList = empService.getEmpList(empParm);
			Emp tempEmp = empList.get(0);
			Integer customerLimit = tempEmp.getCustomerLimitApproval();
			Integer customerLowerLimit = tempEmp.getCustomerLowerLimitApproval();
			Integer isArtificialPush = tempEmp.getIsArtificialPushApproval();
			Integer isSysPush = tempEmp.getIsSysPushApproval();
			Integer receiveCustom = tempEmp.getReceiveCustomApproval();
			Integer repcustomerCount = tempEmp.getRepcustomerCountApproval();
			if(customerLimit == null) {
				customerLimit=tempEmp.getCustomerLimit();
			}
			Integer surplusLimit = customerLimit+(tempEmp.getSurplusLimit()-tempEmp.getCustomerLimit());
			if("1".equals(approvalStatus)){
				emp.setRepcustomerCount(repcustomerCount);
				emp.setCustomerLimit(customerLimit);
				emp.setCustomerLowerLimit(customerLowerLimit);
				emp.setIsSysPush(isSysPush);
				emp.setIsArtificialPush(isArtificialPush);
				emp.setReceiveCustom(receiveCustom);
				emp.setSurplusLimit(surplusLimit);
			}
			emp.setApprovalStatus(Integer.parseInt(approvalStatus));
			empService.updateEmp(emp);
		}
		return "redirect:/ruleSet/approval/employeeSetApproval/employeeSetApprovalPage";
	}
}
